﻿Public Class IO
    Public Shared Function IsSubDir(ByVal childPath As String, ByVal parentPath As String) As Boolean
        If Not childPath.EndsWith(System.IO.Path.PathSeparator) Then childPath = childPath & System.IO.Path.DirectorySeparatorChar
        If Not parentPath.EndsWith(System.IO.Path.PathSeparator) Then parentPath = parentPath & System.IO.Path.DirectorySeparatorChar
        Return childPath.ToLower.StartsWith(parentPath.ToLower)
    End Function

    Public Shared Function ReadTextFile(ByVal fileName As String) As String
        Using tr As New System.IO.StreamReader(fileName)
            Return tr.ReadToEnd()
        End Using
    End Function
    Public Shared Sub SaveTextFileUnicode(ByVal fileName As String, ByVal text As String)
        Using tr As New System.IO.StreamWriter(fileName, False, System.Text.Encoding.Unicode)
            tr.Write(text)
        End Using
    End Sub


    Public Class BlockStreamWriter
        Private mStream As System.IO.Stream
        Public Sub New(ByVal stream As System.IO.Stream)
            mStream = stream
        End Sub

        Public Sub Write(ByVal text As String)
            Dim bText() As Byte = System.Text.Encoding.UTF8.GetBytes(text)
            Write(bText)
        End Sub
        Public Sub Write(ByVal value As Integer)
            For i As Integer = 0 To 3
                Dim iVal As Integer = value Mod 256
                Write(CByte(iVal))
                value = value \ 256
            Next
        End Sub
        Public Sub Write(ByVal data() As Byte)
            Write(mStream, data)
        End Sub
        Public Sub Write(ByVal value As Boolean)
            Write(mStream, value)
        End Sub
        Public Sub Write(ByVal value As Byte)
            mStream.WriteByte(value)
        End Sub
        'Public Sub Write(ByVal value As Short)
        '    For i As Integer = 0 To 1
        '        Dim iVal As Short = CShort(value Mod 256)
        '        Write(CByte(iVal))
        '        value = CShort(value \ 256)
        '    Next
        'End Sub

        Public Shared Sub Write(ByVal stream As System.IO.Stream, ByVal data() As Byte)
            If data Is Nothing Then
                Write(stream, 0)
            ElseIf data.Length = 0 Then
                Write(stream, 0)
            Else
                Write(stream, data.Length)
                stream.Write(data, 0, data.Length)
            End If
        End Sub
        Public Shared Sub Write(ByVal stream As System.IO.Stream, ByVal value As Integer)
            For i As Integer = 0 To 3
                Dim iVal As Integer = value Mod 256
                Write(stream, CByte(iVal))
                value = value \ 256
            Next
        End Sub
        Public Shared Sub Write(ByVal stream As System.IO.Stream, ByVal value As Byte)
            stream.WriteByte(value)
        End Sub
        Public Shared Sub Write(ByVal stream As System.IO.Stream, ByVal value As Boolean)
            stream.WriteByte(CByte(IIf(value, 1, 0)))
        End Sub
        Public Shared Sub Write(ByVal stream As System.IO.Stream, ByVal text As String)
            If text Is Nothing Then text = String.Empty
            Dim bText() As Byte = System.Text.Encoding.UTF8.GetBytes(text)
            Write(stream, bText)
        End Sub


    End Class

    Public Class BlockStreamReader
        Private mStream As System.IO.Stream
        Public Sub New(ByVal stream As System.IO.Stream)
            mStream = stream
        End Sub

        Public Function ReadString() As String
            Return ReadString(mStream)
        End Function

        Public Function ReadBytes() As Byte()
            Return ReadBytes(mStream)
        End Function

        Public Function ReadByte() As Byte
            Return CByte(mStream.ReadByte)
        End Function

        Public Function ReadInteger() As Integer
            Return ReadInteger(mStream)
        End Function
        'Public Function ReadShort() As Short
        '    Return ReadShort(mStream)
        'End Function
        Public Function ReadBoolean() As Boolean
            Return ReadBoolean(mStream)
        End Function

        Public Shared Function ReadInteger(ByVal stream As System.IO.Stream) As Integer
            Dim bData(3) As Integer
            For i As Integer = 0 To 3
                bData(i) = stream.ReadByte
            Next
            Dim iRet As Integer
            For i As Integer = 0 To 3
                Dim iVal As Integer = bData(i)
                iRet += CInt(256 ^ i) * iVal
            Next
            Return iRet
        End Function

        Public Shared Function ReadBytes(ByVal stream As System.IO.Stream) As Byte()
            Dim iLen As Integer = ReadInteger(stream)
            If iLen = 0 Then
                Return New Byte() {}
            Else
                Dim bData(iLen - 1) As Byte
                stream.Read(bData, 0, iLen)
                Return bData
            End If
        End Function

        Public Shared Function ReadString(ByVal stream As System.IO.Stream) As String
            Dim bData() As Byte = ReadBytes(stream)
            Return System.Text.Encoding.UTF8.GetString(bData)
        End Function

        'Public Shared Function ReadShort(ByVal stream As System.IO.Stream) As Short
        '    Dim bData(1) As Integer
        '    For i As Integer = 0 To 1
        '        bData(i) = stream.ReadByte
        '    Next
        '    Dim iRet As Short
        '    For i As Integer = 0 To 1
        '        Dim iVal As Integer = bData(i)
        '        iRet += CShort(CInt(256 ^ i) * iVal)
        '    Next
        '    Return iRet
        'End Function

        Public Shared Function ReadBoolean(ByVal stream As System.IO.Stream) As Boolean
            Return stream.ReadByte = 1
        End Function

    End Class

End Class
